//
//  ZCHomeCellViewModel.swift
//  weibo
//
//  Created by 张城 on 16/7/23.
//  Copyright © 2016年 张城. All rights reserved.
//

import UIKit

class ZCHomeCellViewModel: NSObject {
    
    var homeModel: ZCHomeModel?
    
    var vipImage: UIImage?
    
    var avatarImage: UIImage?
    
    var timeLabeltext: String?{
        
        
        
        return NSDate.sinaDate(homeModel!.created_at ?? "").sinaTimeStr
        
        
    }
    
    var sourceAttr: NSMutableAttributedString?
    
    
    var attitudesCountStr: String?
    
    var commentsCountStr: String?
    
    var repostsCountStr: String?
    
    
    
    init(homeModel:ZCHomeModel) {
        super.init()
        self.homeModel = homeModel
        self.vipImage = getVipImage((homeModel.user?.mbrank)!)
        self.avatarImage = getAvatarImage(homeModel.user!.verified)
        //       self.timeLabeltext = getTime(homeModel.created_at)
        self.sourceAttr = getSourceStr(homeModel.source)
        
        self.repostsCountStr = getCount(homeModel.reposts_count, title: "转发")
        self.commentsCountStr = getCount(homeModel.comments_count, title: "评论")
        self.attitudesCountStr = getCount(homeModel.attitudes_count, title: "赞")
    }
    
}


extension ZCHomeCellViewModel {
    
    
    func getCount(count: Int,title: String) -> String {
        if count <= 0 {
            return title
        }else{
            if count < 10000 {
                return "\(count)"
            }else{
                let sinaCount = CGFloat(count/1000)/10
                var sinaCountStr = "\(sinaCount)万"
                if sinaCountStr.containsString(".0") {
                    sinaCountStr = sinaCountStr.stringByReplacingOccurrencesOfString(".0", withString: "")
                }
                return sinaCountStr
            }
        }
    }
    
    
    // common_icon_membership_level2
    func getVipImage(mbrank: Int?) -> UIImage? {
        guard let mbr = mbrank else {
            return nil
        }
        
        if mbr > 0 && mbr < 7 {
            return UIImage(named: "common_icon_membership_level\(mbr)")
        }
        return UIImage(named: "common_icon_membership")
    }
    
    func getAvatarImage(verified: Int?) -> UIImage? {
        guard let ver = verified else {
            return nil
        }
        
        switch ver {
        case 1:
            return UIImage(named: "avatar_vgirl")
        case 2,3,5:
            return UIImage(named: "avatar_vip")
        case 220:
            return UIImage(named: "avatar_enterprise_vip")
        default:
            return UIImage(named: "avatar_grassroot")
        }
    }
    
//    func getTime(created_at: String?)->String?{
//        guard let creat = created_at else {
//            return nil
//        }
//        
//        let formatt = NSDateFormatter()
//        formatt.dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
//        //        formatt.dateFormat = "yyyy-MM-dd HH:mm:ss"
//        //        formatt.locale = [[NSLocale alloc] initWithLocaleIdentifier:"en_US"]
//        formatt.locale  = NSLocale(localeIdentifier: "en")
//        let date = formatt.dateFromString(creat)
//        //        print(formatt.stringFromDate(date!))
//        var calendar: NSCalendar = NSCalendar.currentCalendar()
//        
//        var unit: NSCalendarUnit = NSCalendarUnit()
//        
//        
//        //        calendar.components(<#T##unitFlags: NSCalendarUnit##NSCalendarUnit#>, fromDate: <#T##NSDate#>)
//        
//        return formatt.stringFromDate(date!)
//    }
    
    func getSourceStr(source: String?)-> NSMutableAttributedString{
        guard let soc = source where soc.containsString("\">") else{
            let str = "来自 arron的微博"
            
            return getSourceAttr(str)
        }
        
        let start = soc.rangeOfString("\">")
        let end = soc.rangeOfString("</")
        
        let result = soc.substringWithRange((start?.endIndex)!..<(end?.startIndex)!)
        
        return getSourceAttr(result)
    }
    
    
    func getSourceAttr(result: String) -> NSMutableAttributedString {
        let name = "来自 " + result
        
        let range = (name as NSString).rangeOfString("来自")
        let attr = NSMutableAttributedString(string: name)
        attr.addAttributes([NSForegroundColorAttributeName:UIColor.grayColor()], range: range)
        return attr
    }
    
    
}
 